home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / pmake / prefix / prefix.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-15  |  12.0 KB  |  359 lines

  1. /***********************************************************************
  2.  *
  3.  * PROJECT:      PMake
  4.  * MODULE:      Prefix -- Main Header File
  5.  * FILE:      prefix.h
  6.  *
  7.  * AUTHOR:        Adam de Boor: Jul  5, 1989
  8.  *
  9.  * REVISION HISTORY:
  10.  *    Date      Name        Description
  11.  *    ----      ----        -----------
  12.  *    7/ 5/89      ardeb        Initial version
  13.  *
  14.  * DESCRIPTION:
  15.  *    Main header file for prefix daemon
  16.  *
  17.  *
  18.  *     $Id: prefix.h,v 1.7 89/10/10 00:36:18 adam Exp $
  19.  *
  20.  *     Copyright (c) Berkeley Softworks 1989
  21.  *     Copyright (c) Adam de Boor 1989
  22.  *
  23.  *     Permission to use, copy, modify, and distribute this
  24.  *     software and its documentation for any non-commercial purpose
  25.  *    and without fee is hereby granted, provided that the above copyright
  26.  *     notice appears in all copies.  Neither Berkeley Softworks nor
  27.  *     Adam de Boor makes any representations about the suitability of this
  28.  *     software for any purpose.  It is provided "as is" without
  29.  *     express or implied warranty.
  30.  *
  31.  ***********************************************************************/
  32. #ifndef _PREFIX_H_
  33. #define _PREFIX_H_
  34.  
  35. #include    <stdio.h>
  36. #include    <sys/types.h>
  37. #include    <sys/stat.h>
  38. #include    <netinet/in.h>
  39.  
  40. /*
  41.  * NFS definitions...Map sun RPC codes with the same name as ours into
  42.  * other codes...
  43.  */
  44. #define        RPC_SUCCESS     SUNRPC_SUCCESS
  45. #define        RPC_CANTSEND    SUNRPC_CANTSEND
  46. #define        RPC_TIMEDOUT    SUNRPC_TIMEDOUT
  47. #define        RPC_TOOBIG        SUNRPC_TOOBIG
  48. #define        RPC_NOPROC        SUNRPC_NOPROC
  49. #define        RPC_ACCESS        SUNRPC_ACCESS
  50. #define        RPC_BADARGS        SUNRPC_BADARGS
  51. #define        RPC_SYSTEMERR   SUNRPC_SYSTEMERR
  52.  
  53. #include    <sys/socket.h>
  54. #include    <rpc/rpc.h>
  55. #include    <sys/ioctl.h>
  56. #include    <sys/dir.h>
  57. #include    <sys/time.h>
  58. #include    <errno.h>
  59. extern int  errno;
  60. #include    <nfs/nfs.h>
  61. #define NFS
  62.  
  63. #undef        RPC_SUCCESS
  64. #undef        RPC_CANTSEND
  65. #undef        RPC_TIMEDOUT
  66. #undef        RPC_TOOBIG
  67. #undef        RPC_NOPROC
  68. #undef        RPC_ACCESS
  69. #undef        RPC_BADARGS
  70. #undef        RPC_SYSTEMERR
  71.  
  72. /*
  73.  * Macro to deal with incompatible calling conventions between gcc and cc on
  74.  * a sparc (gcc passes the address in a register, since the structure is
  75.  * small enough, while cc still passes the address).
  76.  */
  77. #if defined(__GNUC__) && defined(sparc)
  78. #define InetNtoA(addr)    inet_ntoa(&(addr))
  79. #else
  80. #define InetNtoA(addr)    inet_ntoa(addr)
  81. #endif
  82.  
  83. /*
  84.  * Need the Lst library definitions for declarations here...
  85.  * Later include files like to define SUCCESS and FAILURE as enums, so...
  86.  */
  87. #include    "lst.h"
  88. #undef SUCCESS
  89. #undef FAILURE
  90.  
  91. /*
  92.  * Port on which the prefix daemon listens
  93.  */
  94. #define PREFIX_PORT     9127
  95.  
  96. /*
  97.  * Mount point to which prefixes are referred when they should be mounted
  98.  */
  99. #define MOUNT_DIR    "/.prefixmnt"
  100.  
  101. /*
  102.  * Name by which the kernel knows us, in case it wants to complain about
  103.  * a prefix.
  104.  */
  105. #define MOUNT_NAME      "prefix"
  106.  
  107. /*
  108.  * RPC procedures we support/use
  109.  */
  110. #define PREFIX_PING         0          /* See if anyone's home */
  111. #define PREFIX_LOCATE        1          /* Find server of a prefix.
  112.                      * IN: prefix,
  113.                      * OUT: local path */
  114. #define PREFIX_EXPORT        2          /* Export a prefix to the world.
  115.                      * IN: prefix and local path
  116.                      * OUT: nothing */
  117. #define PREFIX_IMPORT        3          /* Import a prefix, but don't mount yet
  118.                      * IN: prefix
  119.                      * OUT: nothing */
  120. #define PREFIX_IMPORT_ROOT  4        /* Import a tree of prefixes
  121.                      * IN: root of tree
  122.                      * OUT: nothing */
  123. #define PREFIX_DELETE        5        /* Delete a prefix/tree (imports only)
  124.                      * IN: prefix
  125.                      * OUT: "Ok" or an error message */
  126. #define PREFIX_DUMP         6        /* Return info about all prefixes
  127.                      * IN: buffer size (short)
  128.                      * OUT: complex buffer described in
  129.                      * DumpPrefix header. */
  130. #define PREFIX_MOUNT           7        /* Mount a prefix on a remote system
  131.                      * IN: prefix descriptor
  132.                      * OUT: 1/0 (long) */
  133. #define PREFIX_MOUNT_LOCAL  8        /* Mount a prefix on the prefix daemon.
  134.                      * IN: address, handle and path
  135.                      * OUT: 1/0 (long) */
  136. #define PREFIX_UNMOUNT         9        /* Unmount a prefix from a remote system
  137.                      * IN: prefix descriptor
  138.                      * OUT: 1/0 (long) */
  139. #define PREFIX_UNMOUNT_LOCAL 10        /* Unmount a prefix mounted on ourselves
  140.                      * IN: path
  141.                      * OUT: 1/0 (long) */
  142. #define PREFIX_DEBUG        11        /* Turn debugging on or off.
  143.                      * IN: flag (long)
  144.                      * OUT: Nothing */
  145. #define PREFIX_NOEXPORT        12        /* Stop exporting a prefix
  146.                      * IN: prefix
  147.                      * OUT: "Ok" or an error message */
  148.  
  149. /*
  150.  * Error codes to supplement the RPC system's own.
  151.  */
  152. #define DUPLICATE_PREFIX    128 /* Error returned by IMPORT and IMPORT_ROOT
  153.                      * if indicated prefix already imported */
  154. #define PREFIX_LOCKED        129 /* Can't do anything with prefix since it's
  155.                      * locked. */
  156.  
  157. /*
  158.  * Structure describing an imported prefix
  159.  */
  160. typedef struct _Prefix {
  161.     char        *path;        /* Mount point */
  162.     int                    generation;     /* Generation number for descriptor */
  163.  
  164.     /*
  165.      * Server info
  166.      */
  167.     struct sockaddr_in     server;        /* Server for prefix */
  168.     char                *serverName;    /* Name of server (for entry into
  169.                      * mount table) NULL if not known */
  170.     char                *remote;        /* Path on server */
  171.  
  172.     /*
  173.      * Other info
  174.      */
  175.     struct nfsfattr    attr;        /* Statistics before we overrode it */
  176.     short        flags;        /* Flags for mount point */
  177. #define PREFIX_ROOT         0x0001         /* Set if prefix is a root (i.e.
  178.                          * not a real prefix, but anything
  179.                          * immediately under it is a viable
  180.                          * prefix) */
  181. #define PREFIX_TEMP         0x0002         /* Temporary prefix created by
  182.                          * looking in a TREE directory */
  183. #define PREFIX_MOUNTED        0x0004        /* Set if prefix mounted on
  184.                          * remote server */
  185.     short        locks;            /* Count of locks on prefix. When
  186.                      * prefix is locked, nothing
  187.                      * should be done with it. Set
  188.                      * when prefix is (a) being
  189.                      * mounted, (b) being unmounted or
  190.                      * (c) being sought. In all these
  191.                      * cases, we are vulnerable due
  192.                      * to the multi-threaded rpc
  193.                      * system */
  194.  
  195.     struct _Prefix      *next;       /* Next/first temporary prefix. Used
  196.                      * for READDIR request on a TREE
  197.                      * prefix */
  198.  
  199.     void        *unmount;    /* Event for unmounting the thing */
  200.  
  201.     char                *options;       /* Mount options */
  202. } Prefix;
  203.  
  204. /*
  205.  * Macros for checking/altering the lock on a prefix. A prefix is locked during
  206.  * a state change: from mounted to unmounted, vice versa, or when it is being
  207.  * sought. In all these cases, we pass control to the Rpc module while the
  208.  * prefix is in flux. While in the Rpc module, we could be given another
  209.  * request to handle for the same prefix. In many cases, doing so will lead
  210.  * to an inconsistent state and, sooner or later, death. Any request that
  211.  * encounters a locked prefix is dropped on the floor to allow the completion
  212.  * of the request that locked the prefix to occur as soon as possible.
  213.  * Were this really multi-threaded, the request could block until the lock
  214.  * was removed. Unfortunately, we can only be a single thread (excluding the
  215.  * child process, of course), so we must return with the request unhandled
  216.  * and rely on the kernel to resubmit later.
  217.  */
  218. #define PrefixLock(pp)    ((pp)->locks++)
  219. #define PrefixUnlock(pp) ((pp)->locks--)
  220. #define PrefixIsLocked(pp) ((pp)->locks != 0)
  221.  
  222. #define PREFIX_STALE    ((Prefix *)-1)
  223.  
  224. /*
  225.  * General file-handle structure returned/passed to kernel. We are given
  226.  * 32 bytes to do with as we will. It doesn't matter what we put here
  227.  * as long as the data will uniquely identify a file. The client side
  228.  * makes absolutely no assumptions as to what is in a file handle,
  229.  * performing byte-by-byte comparisons between two handles when searching
  230.  * for a file.
  231.  *
  232.  * We separate the handles we return into three categories:
  233.  *    regular:    describes an imported prefix.
  234.  *    lookup:        when a file is looked up in a prefix, a token
  235.  *                is created giving the name of the file and the
  236.  *                prefix in which it was sought. A pointer to this
  237.  *                token is returned with a "lookup" handle
  238.  *    special:    the MOUNT_DIR to which the kernel is referred when a
  239.  *                prefix needs to be mounted. Should we ever need another
  240.  *                special directory (I don't anticipate it), we've got 28
  241.  *                more bytes to play with.
  242.  *
  243.  * Note that we don't need any special XDR routine for the encoding/decoding
  244.  * of the handle, since the client pays no attention to the contents and
  245.  * we'll always get it back the same way we sent it out, so it'll be in the
  246.  * correct byte-order. Besides which, we only talk to the local kernel,
  247.  * so how can there possibly be byte-order problems?
  248.  *
  249.  * Note that both prefixes and lookup tokens have a "generation" number.
  250.  * Once allocated, a Prefix or LookupToken structure is never returned
  251.  * to the general memory pool. This allows us to increase the generation
  252.  * number when the structure is "freed" and detect an attempt by the kernel
  253.  * to use a stale handle. Were it not for this pooling and the generation
  254.  * number, we could very easily free a prefix, re-allocate it and have
  255.  * the kernel reference the re-allocated structure and we wouldn't be
  256.  * aware of it. This way, we can detect such mismatches and report an error
  257.  * properly.
  258.  */
  259. typedef enum {
  260.     PH_REGULAR,            /* Regular prefix handle */
  261.     PH_LOOKUP,            /* Lookup inside a prefix */
  262.     PH_SPECIAL,            /* Special mount directory */
  263. } HandleType;
  264.  
  265. typedef union {
  266.     char        buf[NFS_FHSIZE];        /* For proper sizing */
  267.     HandleType    type;
  268.     struct {
  269.     HandleType          type;       /* PH_REGULAR */
  270.     int                 generation; /* Generation number expected in
  271.                      * prefix */
  272.     Prefix                *pp;        /* Prefix involved */
  273.     }    rd;
  274.     struct {
  275.     HandleType          type;       /* PH_LOOKUP */
  276.     int                 generation;    /* Generation number expected in
  277.                      * lookup token */
  278.     struct _LookupToken *ltp;
  279.     }    ld;
  280.     struct {
  281.     HandleType          type;       /* PH_SPECIAL */
  282.     }    sd;
  283. } PrefixHandle;
  284.  
  285. /*
  286.  * Number of seconds between attempts to unmount a prefix (10 minutes)
  287.  */
  288. #define UNMOUNT_INTERVAL    600
  289.  
  290. /******************************************************************************
  291.  *
  292.  *                  EXTERNALS
  293.  *
  294.  *****************************************************************************/
  295.  
  296. /* main.c */
  297. /*
  298.  * List that keeps track of prefixes.
  299.  */
  300. extern Lst        prefixes;
  301.  
  302. /*
  303.  * Miscellaneous variables
  304.  */
  305. extern struct sockaddr_in prefixMountAddr;  /* Address of our prefix NFS server
  306.                          * socket */
  307.  
  308. extern struct nfsfattr  link_attr;      /* Attributes for all symbolic links
  309.                      * we return */
  310.  
  311. extern int          prefixSock;    /* Socket we use to communicate with other
  312.                  * prefix daemons */
  313. extern int          quiet;      /* Don't spew regular messages to the console.
  314.                  * Just spew errors */
  315.  
  316. /*
  317.  * Allocation/freeing of physical Prefix records.
  318.  */
  319. extern Prefix        *AllocPrefix();
  320. extern void         FreePrefix();
  321. /*
  322.  * Converts a pointer to an NFS file handle into a Prefix. Returns
  323.  * PREFIX_STALE if handle is stale.
  324.  */
  325. extern Prefix        *HandleToPrefix();
  326. /*
  327.  * Convert a Prefix into an NFS file handle.
  328.  */
  329. extern void         PrefixToHandle();
  330. /*
  331.  * Converts a path name to a Prefix
  332.  */
  333. extern Prefix        *NameToPrefix();
  334.  
  335. extern void         Message();            /* Write to console */
  336. extern void         dprintf();            /* Write debug info to console */
  337.  
  338. /* mount.c */
  339. extern void         Mount_Init();        /* Initialize Mount stuff */
  340. extern int          Mount_Unmount();        /* Unmount a prefix */
  341. extern int          Mount_MountRoot();        /* Mount the special MOUNT_DIR */
  342.  
  343. /* export.c */
  344. extern void         Export_Init();        /* Initialize Export stuff */
  345. extern void         Export_Send();        /* Tell local daemon to export */
  346.  
  347. /* import.c */
  348. extern void         Import_Init();        /* Initialize Import stuff */
  349. extern int         Import_MountPrefix();   /* Mount prefix on ourselves */
  350. extern Prefix        *Import_CreatePrefix(); /* Create a new Prefix */
  351.  
  352. /* child.c */
  353. extern int          Child_Init();           /* Initialize Child process */
  354. extern int          Child_Call();           /* Call something in the child */
  355. extern int          Child_MountSpecial();   /* Mount a special directory */
  356. extern void         Child_Kill();           /* Kill the child process */
  357.  
  358. #endif /* _PREFIX_H_ */
  359.